package com.laiketui.task.controller;

import com.laiketui.common.api.order.PublicTaskService;
import com.laiketui.common.process.HandlerOrderTaskContext;
import com.laiketui.domain.lktconst.DictionaryConst;
import com.laiketui.task.queue.Consumer;
import com.laiketui.task.queue.Message;
import com.laiketui.task.services.interfaces.*;
import com.laiketui.task.services.interfaces.plugin.FlashsaleService;
import com.laiketui.task.services.interfaces.plugin.SignTaskService;
import com.xxl.job.core.handler.annotation.XxlJob;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * 异步任务api
 *
 * @author Trick
 * @date 2020/12/11 11:17
 */
@Component
public class TaskController {

    @Autowired
    CodeImageTaskService codeImageTaskService;

    @Autowired
    CouponTaskService couponTaskService;

    @Autowired
    MchTaskService mchTaskService;

    @Autowired
    OrderTaskService orderTaskService;

    @Autowired
    TaskService taskService;

    @Autowired
    GoodsTaskService goodsTaskService;

    @Autowired
    HandlerOrderTaskContext handlerOrderTaskContext;

    @Autowired
    private SignTaskService signTaskService;

    @Autowired
    private UserTaskService userTaskService;

    @Autowired
    private APICheckService apiCheckService;

    @Autowired
    private FlashsaleService flashsaleService;

    @Autowired
    private WxTaskService wxTaskService;

    @XxlJob(value = "youtask")
    public void youtask() {
        //定时缓存数据
        System.out.println("hello xxl");
    }

    @XxlJob(value = "storeTask")
    public void storeTask() {
        //定时缓存 有效的商城id
        taskService.taskStoreAll();
    }

    @XxlJob(value = "couponTask")
    public void couponTask() {
        //优惠卷定时任务
        couponTaskService.couponTimingTask();
    }

    @XxlJob(value = "autoPushCoupon")
    public void autoPushCoupon() {
        //优惠卷定自动发放任务
        couponTaskService.autoPushCoupon();
    }

    @XxlJob(value = "extractionCodeTask")
    public void extractionCodeTask() {
        //提取码定时任务
        mchTaskService.extractionCodeTask();
    }


    @XxlJob(value = "orderFailureTask")
    public void orderFailure() {
        //删除失效订单定时任务
        orderTaskService.orderFailure();
    }

    @XxlJob(value = "cancellationShop")
    public void cancellationShop() {
        //定时清理店铺
        taskService.cancellationShop();
    }

    @XxlJob(value = "ptOrderTask")
    public void dotask() {
        //拼团订单定时任务
        orderTaskService.ptOrderTask();
    }

    @XxlJob(value = "AdditionUserData")
    public void AdditionUserData() {
        //定时缓存平台新增用户信息（用户报表）
        userTaskService.AdditionUserData();
    }

    @XxlJob(value = "codeImgClear")
    public void codeImgClear() {
        //定时删除5分钟没有使用的验证码图片
        codeImageTaskService.codeImgClear();
    }

    /**
     * 订单报表定时缓存信息
     */
    @XxlJob(value = "orderReportTask")
    public void saveOrderReport() {
        orderTaskService.saveReportData();
    }

    @XxlJob(value = "taskMS")
    public void taskMs() {
        //秒杀定时任务
        PublicTaskService publicTaskService = handlerOrderTaskContext.getOrderService(DictionaryConst.OrdersType.ORDERS_HEADER_MS + DictionaryConst.TaskType.TASK);
        publicTaskService.execute();
    }

    @XxlJob(value = "taskMSStock")
    public void taskMsStock() {
        //秒杀回滚库存
        PublicTaskService publicTaskService = handlerOrderTaskContext.getOrderService(DictionaryConst.OrdersType.ORDERS_HEADER_MS + DictionaryConst.TaskType.TASK);
        publicTaskService.stock();
    }

    @XxlJob(value = "taskMSCache")
    public void taskMSCache() {
        //缓存秒杀活动信息 -->未使用
        PublicTaskService publicTaskService = handlerOrderTaskContext.getOrderService(DictionaryConst.OrdersType.ORDERS_HEADER_MS + DictionaryConst.TaskType.TASK);
        publicTaskService.Cache();
    }
    @XxlJob(value = "taskJP")
    public void taskJp() {
        //竞拍定时任务 10分钟
        PublicTaskService publicTaskService = handlerOrderTaskContext.getOrderService(DictionaryConst.OrdersType.ORDERS_HEADER_JP + DictionaryConst.TaskType.TASK);
        publicTaskService.execute();
    }

    @XxlJob(value = "taskPT")
    public void taskPt() {
        //拼团定时任务 10分钟
        PublicTaskService publicTaskService = handlerOrderTaskContext.getOrderService(DictionaryConst.OrdersType.ORDERS_HEADER_PT + DictionaryConst.TaskType.TASK);
        publicTaskService.execute();
    }

    @XxlJob(value = "signTask")
    public void signTask() {
        //签到异步任务
        taskService.signTask();
    }


    @XxlJob(value = "taskFX")
    public void taskFx() {
        //分销定时任务
        PublicTaskService publicTaskService = handlerOrderTaskContext.getOrderService(DictionaryConst.OrdersType.ORDERS_HEADER_FX + DictionaryConst.TaskType.TASK);
        publicTaskService.execute();
    }

    @XxlJob(value = "receivingGoods")
    public void receivingGoods() {
        //自动定时自动收货
        orderTaskService.receivingGoods();
    }


    @XxlJob(value = "remindDeliver")
    public void remindDeliver() {
        //重置提醒发货
        mchTaskService.remindDeliver();
    }


    @XxlJob(value = "goodsStatus")
    public void goodsStatus() {
        //刷新商品状态以及库存预警
        goodsTaskService.goodsStatus();
    }

    @XxlJob(value = "sellGoods")
    public void sellGoods() {
        //预售商品及订单定时任务
        goodsTaskService.sellGoods();
    }


    @XxlJob(value = "clearMessageDay")
    public void clearMessageDay() {
        //定时清空消息
        taskService.clearMessageDay();
    }


    @XxlJob(value = "reptilesTaoBao")
    public void reptilesTaoBao() {
        //定时获取淘宝商品
        taskService.reptilesTaoBao();
    }

    @XxlJob(value = "autoGoodComment")
    public void autoGoodComment() {
        //定时自动好评
        mchTaskService.autoGoodComment();
    }

    @XxlJob(value = "resettingPwdNum")
    public void resettingPwdNum() {
        //定时重置登录重试次数
        taskService.resettingPwdNum();
    }


    @XxlJob(value = "orderSettlement")
    public void orderSettlement() {
        //商家定订单时结算
        mchTaskService.orderSettlement();
    }


    @XxlJob(value = "settlementIntegral")
    public void settlementIntegral() {
        //定时结算积分
        taskService.settlementIntegral();
    }

    @XxlJob(value = "memberExpirationProcessing")
    public void memberExpirationProcessing() {
        //会员过期处理
        taskService.memberExpirationProcessing();
    }

    @XxlJob(value = "mchAutoExamine")
    public void mchAutoExamine() {
        //店铺自动审核成功
        taskService.mchAutoExamine();
    }

    @XxlJob(value = "mchAutoLogOff")
    public void mchAutoLogOff() {
        //店铺自动注销
        taskService.mchAutoLogOff();
    }

    @XxlJob(value = "distributionSettlement")
    public void distributionSettlement() {
        //分销订单确认收货佣金结算特殊处理(订单参数格式:FX23082415490904540,FX230904090924611042,...)
        orderTaskService.distributionSettlement();
    }

    @XxlJob(value = "storeExpiration")
    public void storeExpiration() {
        //处理商城过期
        taskService.storeExpiration();
    }


    @XxlJob(value = "checkApis")
    public void checkApis() {
        //监测apis是否可用 10分钟检查一次
        apiCheckService.checkApis();
        //TODO 服务重启
    }

    @XxlJob(value = "flashsaleActivity")
    public void  flashsaleActivity() {
        //限时折扣活动定时任务
        flashsaleService.activityTask();
    }

    @XxlJob(value = "V3QueryBatchTransferOrder")
    public void  V3QueryBatchTransferOrder() {
        //商家转账到零钱查询批次转账单结果
        wxTaskService.V3QueryBatchTransferOrder();
    }

    @XxlJob(value = "distributionIncome")
    public void  distributionIncome() {
        //分销系统收益统计报表
        taskService.distributionIncome();
    }

    @XxlJob(value = "synchronizationWxDelivery")
    public void  synchronizationWxDelivery() {
        //同步微信发货信息录入的物流公司信息
        wxTaskService.synchronizationWxDelivery();
    }

    @XxlJob(value = "synchronizationWxOrderStatus")
    public void  synchronizationWxOrderStatus() {
        //同步微信发货订单状态
        wxTaskService.synchronizationWxOrderStatus();
    }

    public static void main(String[] args) throws InterruptedException {
        // 添加延时消息,m1 延时
        Message m1 = new Message(1, "world", 3);
        Message m3 = new Message(3, "test", 6);
        // 添加延时消息,m2 延时
        Message m2 = new Message(2, "hello", 10);
        //将延时消息放到延时队列中
        Consumer.QUEUE.add(m2);
        Consumer.QUEUE.add(m1);
        Consumer.QUEUE.add(m3);
        // 启动消费线程 消费添加到延时队列中的消息，前提是任务到了延期时间
        ExecutorService exec = Executors.newFixedThreadPool(3);
        exec.execute(() -> {
            Message take;
            try {
                take = Consumer.QUEUE.take();
                System.out.println("消费消息id：" + take.getId() + " 消息体：" + take.getBody());
                take = Consumer.QUEUE.take();
                System.out.println("消费消息id：" + take.getId() + " 消息体：" + take.getBody());
                System.out.println(Consumer.QUEUE.size());
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });

        exec.shutdown();
    }
}
